{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Test OpenAI SDK with Byzer-LLM Chat API\n",
    "\n",
    "This notebook demonstrates how to use the OpenAI SDK to call Byzer-LLM's chat completion API endpoint."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import openai\n",
    "import os\n",
    "\n",
    "# Configure OpenAI client to use our local server\n",
    "client = openai.OpenAI(\n",
    "    api_key=\"dummy\",  # The API key can be any string when using local server\n",
    "    base_url=\"http://localhost:8000/v1\"  # Point to local Byzer-LLM serve endpoint\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "ChatCompletion(id='test-123', choices=[Choice(finish_reason=None, index=0, logprobs=None, message=ChatCompletionMessage(content='I am DeepSeek Chat, an intelligent assistant developed by DeepSeek Corporation, designed', role='assistant', function_call=None, tool_calls=None))], created=1730955872, model='deepseek_chat', object='chat.completion', service_tier=None, system_fingerprint=None, usage=CompletionUsage(completion_tokens=16, prompt_tokens=9, total_tokens=25))\n",
      "Assistant: I am DeepSeek Chat, an intelligent assistant developed by DeepSeek Corporation, designed\n"
     ]
    }
   ],
   "source": [
    "# Test basic chat completion\n",
    "response = client.chat.completions.create(\n",
    "    model=\"deepseek_chat\",  \n",
    "    messages=[\n",
    "        {\"role\": \"user\", \"content\": \"Hello! Who are you?\"}\n",
    "    ],\n",
    "    extra_body={\"request_id\": \"test-123\"}\n",
    ")\n",
    "print(response)\n",
    "print(f\"Assistant: {response.choices[0].message.content}\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Assistant: Certainly! Let's consider a simple example of machine learning in action: email\n"
     ]
    }
   ],
   "source": [
    "# Test multi-turn conversation\n",
    "response = client.chat.completions.create(\n",
    "    model=\"deepseek_chat\",\n",
    "    messages=[\n",
    "        {\"role\": \"user\", \"content\": \"Can you explain what machine learning is?\"},\n",
    "        {\"role\": \"assistant\", \"content\": \"Machine learning is...\"},\n",
    "        {\"role\": \"user\", \"content\": \"Can you give an example?\"}\n",
    "    ]\n",
    ")\n",
    "\n",
    "print(f\"Assistant: {response.choices[0].message.content}\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Streaming response chunks:\n",
      "Hello! I'm an AI assistant developed by DeepSeek Company, specifically designed to provide information inquiry, dialogue communication, and problem-solving services. How can I assist you today?\n"
     ]
    }
   ],
   "source": [
    "# Test streaming chat completion\n",
    "print(\"Streaming response chunks:\")\n",
    "stream = client.chat.completions.create(\n",
    "    model=\"deepseek_chat\",\n",
    "    messages=[{\"role\": \"user\", \"content\": \"Hello! Who are you?\"}],\n",
    "    stream=True,\n",
    "    max_tokens=1000\n",
    ")\n",
    "\n",
    "for chunk in stream:\n",
    "    if chunk.choices[0].delta.content is not None:\n",
    "        print(chunk.choices[0].delta.content, end=\"\")\n",
    "print()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.11"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
